require "bit"
mm = 10
local nextHH, nextMM
nextHH = bit.band((mm / 60), 0xff)
nextMM = mm % 60
-- print('TEST', nextHH, nextMM)

a = false and 200 or 100
-- print(a)

-- assert 断言测试

function assertTest(...)
    assert(10 > 20, '大小弄错了把')
    print('不知道这个执行不执行') -- 抛出异常，不会执行了
end
-- os 函数测试

local logDate = os.date("%Y-%m-%d %H:%M:%S")
-- print(logDate)
-- print(string.sub('123456789',-3))
-- print(string.sub([[1234
-- 56789]],-3))
--[[ voltValue = 334
val = (voltValue - voltValue % 3) / 3
print(val)
val = voltValue/3.3
print(val)
 ]]

--[[ list = {1,2,3,4}
print(unpack(list)) ]]

-- print(tonumber("")) --nil
-- print(tostring(0xa2))

-- print(string.unpack("1234"))
-- print("aa"..nil)

--[[ function f()
    return
end
print(f().."a")
 ]]
--[[ for i = 1, 0 do
    print("111"..i)
end
 ]]

-- 备注：math.modf() 这个函数就是为了获取除法的整数部分
function hexToFloat(hexString)
    if hexString == nil then return 0 end
    local t = type(hexString)
    if t == "string" then hexString = tonumber(hexString, 16) end
    local hexNums = hexString
    local sign = math.modf(hexNums / (2 ^ 31))
    local exponent = hexNums % (2 ^ 31)
    exponent = math.modf(exponent / (2 ^ 23)) - 127
    local mantissa = hexNums % (2 ^ 23)
    for i = 1, 23 do mantissa = mantissa / 2 end
    mantissa = 1 + mantissa
    local result = (-1) ^ sign * mantissa * 2 ^ exponent
    return result
end
function FloatToHex(floatNum)
    local S = 0
    local E = 0
    local M = 0
    if floatNum == 0 then return "00000000" end
    local num1, num2 = math.modf(floatNum / 1)
    local InterPart = num1
    if floatNum > 0 then
        S = 0 * 2 ^ 31
    else
        S = 1 * 2 ^ 31
    end
    local intercount = 0
    repeat
        num1 = math.modf(num1 / 2)
        intercount = intercount + 1
    until (num1 == 0)
    E = intercount - 1
    local Elen = 23 - E
    InterPart = InterPart % (2 ^ E)
    InterPart = InterPart * (2 ^ Elen)
    E = E + 127
    E = E * 2 ^ 23
    for i = 1, Elen do
        num2 = num2 * 2
        num1, num2 = math.modf(num2 / 1)
        M = M + num1 * 2 ^ (Elen - i)
    end
    M = InterPart + M

    -- E值为整数部分转成二进制数后左移位数：22.8125 转成二进制10110.1101，左移4位 1.01101101
    -- E=4 ，再加上127 就为所需E值
    -- 010000011 01101101 000000000000000
    local Result = S + E + M
    Result = string.format("%08X", Result)
    return Result
end

function string.toHex(str, separator) return str:gsub('.', function(c) return string.format("%02X" .. (separator or ""), string.byte(c)) end) end
--- 将HEX字符串转成Lua字符串，如"313233616263"转为"123abc", 函数里加入了过滤分隔符，可以过滤掉大部分分隔符（可参见正则表达式中\s和\p的范围）。
-- @string hex,16进制组成的串
-- @return charstring,字符组成的串
-- @return len,输出字符串的长度
-- @usage
-- string.fromHex("010203")       ->  "\1\2\3"
-- string.fromHex("313233616263") ->  "123abc"
function string.fromHex(hex) return hex:gsub("[%s%p]", ""):gsub("%x%x", function(c) return string.char(tonumber(c, 16)) end) end
-- 顺时，  float, 回复数据:010304851F4145135A 
-- 正累计，float ,回复数据:01030487DD4145B31E
data = string.fromHex("010304851F4145135A")
--[[ print(data, #data)
print(data:byte(6) * 2 ^ 24 + data:byte(7) * 2 ^ 16 + data:byte(4) * 2 ^ 8 + data:byte(5) + 0.0)
meter_ins = string.format("%.2f", data:byte(6) * 2 ^ 24 + data:byte(7) * 2 ^ 16 + data:byte(4) * 2 ^ 8 + data:byte(5))
data = string.fromHex("01030487DD4145B31E")
print(data:byte(6) * 2 ^ 24 + data:byte(7) * 2 ^ 16 + data:byte(4) * 2 ^ 8 + data:byte(5))
meter_val = string.format("%.2f", data:byte(6) * 2 ^ 24 + data:byte(7) * 2 ^ 16 + data:byte(4) * 2 ^ 8 + data:byte(5))
print(meter_val, meter_ins)
 ]]
-- print(unpack("f",string.fromHex("4145851F")))
-- print(table.pack({0x41,0x45,0x85,0x1F}))
-- temp = (data:sub(6,7)..data:sub(4,5)):toHex()
-- print(string.format("%.2f",hexToFloat("9A1B383B")))
d = {123}
function valToBCD(val) -- 1548870->7088540100
    temp = tonumber(val)
    print(tonumber(d[2] or 0) * 1000)
    if not temp then return "0000000000" end
    temp = string.format("%010d", math.floor(temp))
    res = temp:sub(9, 10) .. temp:sub(7, 8) .. temp:sub(5, 6) .. temp:sub(3, 4) .. temp:sub(1, 2)
    return res
end
-- print(valToBCD(0))

local function valToBCD2(v, len)
    if v < 0 then v = 0 end
    return string.format("%0" .. len .. "d", math.floor(v))
end
--print(valToBCD2(12.89, 4))
dataHex = "B34104050140C00000000000708854010000003000472615173C"
len = string.format("%02X",#dataHex/2)
--print(len)

--print(tonumber(""))
function string.split(str, delimiter)
    local strlist, tmp = {}, string.byte(delimiter)
    if delimiter and delimiter == "" then
        for i = 1, #str do strlist[i] = str:sub(i, i) end
    elseif type(delimiter) == "number" then
        for i = 1, #str, delimiter do table.insert(strlist, str:sub(i, i + delimiter - 1)) end
    else
        for substr in string.gmatch(str .. delimiter, "(.-)" .. (((tmp > 96 and tmp < 123) or (tmp > 64 and tmp < 91) or (tmp > 47 and tmp < 58)) and delimiter or "%" .. delimiter)) do
            table.insert(strlist, substr)
        end
    end
    return strlist
end
local str = "v1,v2,v3,v4"
local t = string.split(str,",")
print("size:"..#t)

table.insert(t,"vx")
print(unpack(t))

print(table.remove(t))
print(table.remove(t))
print("size:"..#t)
print(unpack(t))
